<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script src="./lib/vue.js"></script>
</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>用户管理</title>
  <script src="./lib/vue.js"></script>
  <script src="./lib/http.js"></script>
</head>

<body>
  <div id="app">
    <div>
      <input type="text" v-model="name" @keyup.enter="onEnter">
    </div>
    <hr>
    <ul>
      <!-- 条件渲染 -->
      <li v-if="users.length==0">请求中...</li>
      <template v-else>
        <li v-for="item in users" :key="item.id">{{item.name}}</li>
      </template>
    </ul>
  </div>

  <script>
    let config = {
      // 用户列表
      users: '/api/users'
    };

    let vm = new Vue({
      el: '#app',
      data: {
        // 初始数据源
        users: [],
        name: ''
      },
      methods: {
        async onEnter() {
          let ret = await request(config.users, { name: this.name })
          this.users.push(ret.data)
          this.name = ''
        }
      },
      // 生命周期中完成网络请求
      async mounted() {
        let ret = await request(config.users)
        this.users = ret.data
      }
    })
  </script>
</body>

</html>